package com.kcs.fxmargin.action;

import java.sql.Date;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;
import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.action.form.FXEndOfDayActionForm;
import com.kcs.fxmargin.bo.FXEndOfDayBO;
import com.kcs.fxmargin.bo.InquiryAccountWSBO;
import com.kcs.fxmargin.service.FXEndOfDayService;
import com.kcs.fxmargin.service.FilesService;
import com.kcs.fxmargin.ws.InquiryAccountWS;

public class FXEndOfDayAction extends FXAction {
	
	private Logger log = Logger.getLogger(Constant.LOG_FX);
//	private Logger logDealCenter = Logger.getLogger(Constant.LOG_DEAL_CENTER);
	private Logger logCbs = Logger.getLogger(Constant.LOG_CBS);
//	private Logger logRetad = Logger.getLogger(Constant.LOG_RETAD);
//	private Logger logImportRate = Logger.getLogger(Constant.LOG_IMPORT_RATE);
	
	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		FXEndOfDayActionForm bean = null;
		FXEndOfDayService svc = null;
		FXEndOfDayBO bo = null;
		InquiryAccountWS ws = null;
		InquiryAccountWSBO wsBo = null;
		
		try{
			
//			logImportRate.info("logImportRate : info");
//			logImportRate.debug("logImportRate : debug");
//			logImportRate.error("logImportRate : error");
//			logImportRate.warn("logImportRate : warn");
//			logImportRate.fatal("logImportRate : fatal");
			
	    	bean = (FXEndOfDayActionForm)form;
	    	svc = new FXEndOfDayService();
	    	
	    	if(bean.getTranCode().equals("")){
	    		bean.resetForm();
	    	}else if(bean.getTranCode().equals("runFXEndOfDay")){
	    		svc.runEndOfDay();

	    	}else if(bean.getTranCode().equals("callDealsCenter")){
	    		svc.callDealCenter();
	    		request.setAttribute("resultMessage", "Call DealsCenter success.");
	    		
	    	}else if(bean.getTranCode().equals("testLog")){
	    		log.debug("------ debug message");
	    		log.info("------ info message");
	    		log.warn("------ warn message");
	    		log.error("------ error message");
	    		log.fatal("------ fatal message");
	    		
	    	}else if(bean.getTranCode().equals("callArchiveData")){
	    		bo = new FXEndOfDayBO();
	    		bo.setStartDate(convertStringToDate(bean.getInStartDate()));
	    		bo.setEndDate(convertStringToDate(bean.getInEndDate()));
	    		svc.callArchiveData(bo);
	    		FilesService.deleteLogFile();
	    		
	    	}else if(bean.getTranCode().equals("testFileService")){
	    		FilesService.deleteFile();
	    		
	    	}else if(bean.getTranCode().equals("callDealsCenter")){
	    		svc.callDealCenter();
	    		
	    	}else if(bean.getTranCode().equals("callArchiveData")){
	    		bo = new FXEndOfDayBO();
	    		bo.setStartDate(convertStringToDate(bean.getInStartDate()));
	    		bo.setEndDate(convertStringToDate(bean.getInEndDate()));
	    		svc.callArchiveData(bo);
	    		
	    	}else if(bean.getTranCode().equals("inquiryAccountStatement")){
	    		wsBo = new InquiryAccountWSBO();
	    		ws = new InquiryAccountWS();
	    		wsBo.setCifNo(bean.getInWsCifNo());
	    		wsBo.setMarginAccNo(bean.getInWsAccNo());
	    		wsBo.setStartDate(convertWsDateFormat(bean.getInWsStartDate()));
	    		wsBo.setEndDate(convertWsDateFormat(bean.getInWsEndDate()));
	    		wsBo.setUser(bean.getUserLoginId());
	    		log.info(">>>>> inquiryAccountStatementTest : Begin");
	    		ws.inquiryAccountStatementTest(wsBo);
	    		log.info("<<<<< inquiryAccountStatementTest : End");
	    		
	    	}else if("testCBS".equals(bean.getTranCode())){
	    		logCbs.info("CIF No 			= " + bean.getCifNo());
	    		logCbs.info("As of Date 		= " + bean.getAsOfDate());
	    		logCbs.info("Margin A/C Code 	= " + bean.getMarginAccCode());
	    		
	    		boolean result = svc.testConnectionCBS();
	    		logCbs.info("Test CBS = "+ result);
	    		request.setAttribute("testCBS", result);

	    	}else if("testOracle".equals(bean.getTranCode())){
	    		boolean result = svc.testConnectionOracle();
	    		log.info("Test Oracle = " + result);
	    		request.setAttribute("testOracle", result);
	    	}
		    	
		}catch(Exception e){
		    	e.printStackTrace();
		    	request.setAttribute(Constant.ERR_ATTR, e+"");
		}finally{
			bean.setTranCode("");
		}
		
		return mapping.findForward("fxEndOfDay");
	}
	
	private Date convertStringToDate(String s)throws Exception{
		Calendar c = null;
		String spr[] = null;
		Date result = null;
				
		if(s != null && !s.equals("")){
			c = Calendar.getInstance();
			spr = s.split("/");
			
			c.set(Calendar.DATE, Integer.parseInt(spr[0]));
			c.set(Calendar.MONTH, Integer.parseInt(spr[1]));
			c.set(Calendar.YEAR, Integer.parseInt(spr[2]));
			
			result = commonUtils.convertUtilDatetoSQLDate(c.getTime());
		}
		
		
		return result;
	}
	
	private String convertWsDateFormat(String s)throws Exception{
		String result = null;
		String sprt[] = null;
		
		if(s != null && !s.equals("")){
			sprt = s.split("/");
			result = sprt[2]+sprt[1]+sprt[0];
		}
		
		return result;
	}

}
